<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Actions\Extensions\ShopClassifyConfigButton;
use App\Admin\Repositories\Shop\WxShopClassify;
use App\Models\Shop\WxShopClassify as ShopClassify;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxShopClassifyController extends AdminController
{
    private $options = [0=>'不显示', 1=>'总是显示', 2=>'审核时不显示'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxShopClassify(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->where('depth', 3)->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'name','intro'])->placeholder('搜索分类ID，名字，介绍...');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('pic')->image('', 60);
            $grid->column('bg_img')->image('', 60);
            $grid->column('intro');
            $grid->column('guarantee')->editable();
            $grid->column('platform_division')->editable();
            $grid->column('parent_id')->select(ShopClassify::pluck('name', 'id'));
            $grid->column('sort')->editable();
//            $grid->column('is_highlight', '高亮')->switch();
//            $grid->column('is_hot', '热榜')->switch();
            $grid->column('state')->switch();


            $grid->column('in_h5')->select($this->options);
            $grid->column('in_app')->select($this->options);
            $grid->column('in_mp')->select($this->options);
//            $grid->column('created_at');

//            $grid->fixColumns(1);
            $grid->model()->orderBy('id', 'asc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->tools([
               new ShopClassifyConfigButton()
            ]);
            $grid->header(function ($collection) {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block;margin-top: 20px;">
小提示: 该界面只显示第三层次的商品分类，需要先去“三层结构配置”，把需要的分类拖到第三层次，然后保存</div>';
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopClassify(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('intro');
            $show->field('pic');
            $show->field('bg_img');

            $show->field('guarantee');
            $show->field('platform_division');

            $show->field('is_shop_type1');
            $show->field('is_shop_type2');
            $show->field('is_shop_type3');
            $show->field('is_shop_type4');

            $show->field('parent_id');
            $show->field('sort');
            $show->field('state');
            $show->field('in_h5');
            $show->field('in_app');
            $show->field('in_mp');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShopClassify(), function (Form $form) {
            $form->display('id');
            $options = ShopClassify::where('depth', '<>', 3)->pluck('name', 'id')->toArray();
            $options[0] = '顶级分类';
            $form->select('parent_id', '父级分类')->help('第一层级的请选择“顶级分类”')->options($options)->default(0);

            $form->text('name');
            $form->text('intro');
            $form->number('guarantee');
            $form->rate('platform_division');

            $form->switch('is_shop_type1')->default(1);
            $form->switch('is_shop_type2')->default(1);
            $form->switch('is_shop_type3')->default(1);
            $form->switch('is_shop_type4')->default(1);


            $form->image('pic')->help('建议尺寸：750*750')
                ->url('files/uploads')->uniqueName()->autoUpload();
            $form->image('bg_img')->help('现在用在类目榜单的位置，宽高比：2.7')
                ->url('files/uploads')->uniqueName()->autoUpload();
            $form->number('sort');

//            $form->switch('is_highlight', '高亮');
//            $form->switch('is_hot', '热榜');


            $form->radio('in_h5')->options($this->options)->default(1);
            $form->radio('in_app')->options($this->options)->default(1);
            $form->radio('in_mp')->options($this->options)->default(1);

            $form->switch('state')->default(1);

            $form->saving(function (Form $form){
                if($form->input('platform_division') > 100){
                    return $form->response()->error('平台分成你设置超过100做什么？');
                }
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
